﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;
using System.Collections;

namespace Joyintech.Admin.inpiao365.general.dianin.trailer
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //绑定电影列表
                DataBindMovie();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("movieTrailer_Id") != "")
                {
                    DataBindForm();
                }
            }
        }

        #region 绑定电影列表

        /// <summary>
        /// 绑定电影列表
        /// </summary>
        private void DataBindMovie()
        {
            List<movie> list = new List<movie>();
            list = new movie_BLL().GetModelList("1=1 order by MovieId desc");

            if (list != null && list.Count > 0)
            {
                this.ddlMovieId.DataSource = list;
                this.ddlMovieId.DataTextField = "MovieName";
                this.ddlMovieId.DataValueField = "MovieId";
                this.ddlMovieId.DataBind();
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new movieTrailer_BLL().GetList("movieId=" + DNTRequest.GetQueryString("movieTrailer_Id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表movie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            movieTrailer model = new movieTrailer();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("movieTrailer_Id") != "")
            {
                model.MovieId = Convert.ToInt32(DNTRequest.GetQueryString("movieTrailer_Id").ToString());

                dataResult = new movieTrailer_BLL().Update(model);
            }
            else
            {
                dataResult = new movieTrailer_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
    }
}